package com.tcp2.frame;

import javax.swing.*;
import javax.swing.border.TitledBorder;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.TimerTask;
import java.util.Vector;

/**
 * Author: <张锦煌>
 * Time: 2015-12-03 09:14
 * Copyright (C) 2015 Xiamen Yaxon Networks CO.,LTD.
 * 操作日志显示界面
 */
public class LogFrame extends JFrame {

    private JTable table1 = new JTable();
    private Vector vector = new Vector();
    private static final java.util.Timer time = new java.util.Timer("test");

    public JPanel logFrame() {
        //操作日志
        JPanel jp4 = new JPanel();
        TitledBorder logBorder = new TitledBorder("操作日志");
        jp4.setBorder(logBorder);
        Vector n1 = new Vector();
        n1.addElement("日志来源");
        n1.addElement("日志描述");
        n1.addElement("日期");
        n1.addElement("消息ID");
        n1.addElement("流水号");
        n1.addElement("原始数据");
        table1 = new JTable();
        table1.setPreferredScrollableViewportSize(new Dimension(800, 160));
        JScrollPane ss = new JScrollPane(table1);
        jp4.add(ss);

        DefaultTableModel tb = new DefaultTableModel(vector,n1){  //重写方法设置表格双击不可编辑
            public boolean isCellEditable(int row, int column)
            {
                return false;
            }
        };
        table1.setModel(tb);
        table1.getColumnModel().getColumn(4).setPreferredWidth(20);
        table1.getColumnModel().getColumn(3).setPreferredWidth(30);
        table1.getColumnModel().getColumn(1).setPreferredWidth(150);

        time.scheduleAtFixedRate(new TimerTask() {
            @Override
            public void run() {
                new SwingWorker<Object, Object>() {
                    java.util.List list1= null;
                    protected Object doInBackground() throws Exception {
                        if (list1!=null) {
                            Vector v = new Vector();
                            v.addAll(list1);
                            vector.insertElementAt(v, 0); //逆序添加
                            return vector;
                        } else {
                            return null;
                        }
                    }
                    protected void done() {
                        if(list1!=null)
                            ((DefaultTableModel) table1.getModel()).fireTableDataChanged();
                    }
                }.execute();
            }
        }, 0,1);

        table1.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                int clickCount = e.getClickCount();
                if (clickCount == 2) {
                    JFrame jf2 = new JFrame("查看详细信息");
                    jf2.setSize(350, 200);
                    jf2.setVisible(true);
                    jf2.setLocationRelativeTo(null); // 初始在屏幕中央
                    jf2.setDefaultCloseOperation(jf2.HIDE_ON_CLOSE);
                    String string = table1.getValueAt(table1.getSelectedRow(), 1)
                            +"\n"+table1.getValueAt(table1.getSelectedRow(), 5);
                    JEditorPane editorPane = new JEditorPane("text/plain", string);
                    editorPane.setBackground(Color.lightGray);
                    editorPane.setEditable(false);
                    jf2.setContentPane(new JScrollPane(editorPane));
                    jf2.setVisible(true);
                }
            }
        });
        return jp4;
    }

}
